import {
	login,
	getInfo,
	loginByWechat
} from '@/api/user.js'
import setting from '@/settings.js'

const state = {
	token: uni.getStorageSync('token'),
	username: '',
	realName: uni.getStorageSync('username'),
	avatar: uni.getStorageSync('avatar'),
	roles: []
}

const mutations = {
	set_token(state, token) {
		state.token = token;
	},
	set_username(state, username) {
		state.username = username;
	},
	set_realName(state, realName) {
		state.realName = realName
	},
	set_avatar(state, avatar) {
		state.avatar = avatar
	},
	set_roles(state, roles) {
		state.roles = roles
	}
}

const actions = {
	// 用户登录
	login({
		commit,
		dispatch
	}, user) {
		return new Promise((resolve, reject) => {
			login(user).then(res => {
				uni.showToast({
					title: "登录成功!",
					icon: "none"
				});
				commit('set_token', res.data.token);
				uni.setStorageSync('token', res.data.token)
				dispatch('getInfo')
				resolve(true)
			}).catch(err => {
				reject(err)
			})
		})
	},
	// 微信登录
	loginByWechat({
		commit,
		dispatch
	}, code) {
		return new Promise((resolve, reject) => {
			loginByWechat(code).then(res => {
				uni.showToast({
					title: "登录成功!",
					icon: "none"
				});
				commit('set_token', res.data.token);
				uni.setStorageSync('token', res.data.token)
				dispatch('getInfo')
				resolve(true)
			}).catch(err => {
				reject(err)
			})
		})
	},
	// 获取用户信息
	getInfo({
		commit
	}) {
		return new Promise((resolve, reject) => {
			getInfo().then(response => {
				const {
					data
				} = response
				if (!data) {
					reject('获取用户信息失败，请重新登录.')
				}

				// 存用户信息
				if(data.realName != null) {
					uni.setStorageSync('username', data.realName)
				} else {
					uni.setStorageSync('username', data.username)
				}
				commit('set_realName', data.realName)
				commit('set_username', data.username)
				commit('set_roles', data.roleCode)
				const avatar = setting.avatarUrl + data.avatar
				uni.setStorageSync('avatar', avatar)
				commit('set_avatar', avatar)
				resolve(data)
			}).catch(error => {
				reject(error)
			})
		})
	},

	// 用户注销登录
	logout({
		state,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			dispatch('resetToken').then(() => {
				resolve()
			}).catch(error => {
				reject(error)
			})
			// logout(state.token).then(async () => {
				
			// 	resolve()
			// }).catch(error => {
			// 	reject(error)
			// })
		})
	},

	// 删除 token
	resetToken({
		commit,
		dispatch
	}) {
		return new Promise(resolve => {
			commit('set_roles', [])
			commit('set_token', '')
			commit('set_username', '')
			commit('set_realName', '')
			commit('set_avatar', '')
			uni.removeStorageSync('token')
			uni.removeStorageSync('username')
			uni.removeStorageSync('avatar')
			resolve()
		})
	},
	
	// 设置头像信息
	setAvatar({ commit }, avatarName) {
		return new Promise(resolve => {
			const avatar = setting.avatarUrl + avatarName
			uni.setStorageSync('avatar', avatar)
			commit('set_avatar', avatar)
			resolve()
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
